/**
 * @auth:Ning
 * purpose: SOUND ITEM, FOR SOUND PLAY...
 * date:2009-5-30	
 *  
 * */
package com.components.media
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	import gs.TweenLite;

	public class SoundPlayer extends EventDispatcher
	{
		//START...
		
		
		//PROPERTY...
		
		private var sndChannel:SoundChannel;
		
		private var _volume:Number = -1;
		
		private var _pan:Number = -1;
		
		private var loop:uint;
		
		private var playingSound:Sound;
		
		private var currentCount:uint;		
		
		public var checkPolicy:Boolean = true;
		
		//CONSTURCOTR....
		public function SoundPlayer()
		{
		
			
		}
		
		//METHODS...
		
		// sound ease...
		private function soundEase(ease:SoundEaseMangement,onComplete:Function = null,mandy:Boolean = false):void
		{
			
			if(ease is SoundEaseMangement)
			{
				
				var objOver:Object = new Object;
				objOver.overwrite = 0;
				
				if(onComplete is Function)	objOver["onComplete"] = onComplete;
								
				for(var i:int = 0;i < ease.props.length;i++)
				{
										
					if(mandy || this[ease.props[i]] < Math.min(ease.values[i][0],ease.values[i][1]) || this[ease.props[i]] > Math.max(ease.values[i][0],ease.values[i][1]))
					{
												
						this[ease.props[i]] = ease.values[i][0];	
						
					}
					
					objOver[ease.props[i]] = ease.values[i][1];
					
				}
				
				new TweenLite(this,ease.duration,objOver);
								
			}
						
		}
		
		// on snd complete...
		private function onSndComplete(evt:Event):void
		{
			
			currentCount++;
			
			if(currentCount >= loop && loop > 0)
			{
				
				sndChannel.stop();
				
				return;
					
			}
			
			playingSound = new Sound(new URLRequest(playingSound.url),new SoundLoaderContext(1000,checkPolicy));
			playingSound.addEventListener(IOErrorEvent.IO_ERROR,function(evt:Event):void{});
					
			sndChannel = playingSound.play();
			volume = volume;
			
			sndChannel.addEventListener(Event.SOUND_COMPLETE,onSndComplete);
									
		}
		
		//play methods....
		public function play(url:String,ease:SoundEaseMangement = null,loop:uint = 0):void
		{
			
			if(sndChannel is SoundChannel)
			{
				
				sndChannel.stop();	
				
			}
			
			loop = loop;
			
			currentCount = 0;
					
			playingSound = new Sound(new URLRequest(url),new SoundLoaderContext(1000,true));
					
			sndChannel = playingSound.play();
			
			try{
			
				sndChannel.addEventListener(Event.SOUND_COMPLETE,onSndComplete);
				
				soundEase(ease,null,true);						
			
			}catch(err:Error){}
						
		}
		
		
		//stop...
		public function stop(ease:SoundEaseMangement = null):void
		{
			
			if(sndChannel)
			{
				
				if(ease is SoundEaseMangement)
				{
					
					soundEase(ease,stop);
					
					return;
					
				}
				
				sndChannel.stop();
																
			}
						
		}
		
		//set volume...
		public function set volume(val:Number):void
		{
			
			_volume = val;
			
			if(sndChannel is SoundChannel)
			{
				
				var sndTR:SoundTransform = new SoundTransform;
				
				sndTR.volume = volume;
				
				sndChannel.soundTransform = sndTR;
				
			}
						
		}
		
		//get volume...
		public function get volume():Number
		{
			
			return _volume;
						
		}
		
		
		//set volume...
		public function set pan(val:Number):void
		{
			
			_pan = val;
			
			if(sndChannel is SoundChannel)
			{
				
				var sndTR:SoundTransform = new SoundTransform;
				
				sndTR.pan = pan;
				
				sndChannel.soundTransform = sndTR;
				
			}
						
		}
		
		//get volume...
		public function get pan():Number
		{
			
			return _pan;
						
		}				
		
		public function clearEase():void
		{
			TweenLite.killTweensOf(this);
		}
		

		//END...
	}
}